package hw02;

import hw01.Answers;

import java.io.IOException;
import java.lang.reflect.Constructor;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class RoomBoss
 */
public class RoomBoss extends HttpServlet {
	
	private static final long serialVersionUID = 2321410645025826278L;

	public static final Answers roomboss = new Answers();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doMethod(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doMethod(request, response);
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doMethod(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		WebPage webPage;
		String webPageClassName = request.getParameter("q");
		try {
			Class<? extends WebPage> webPageClass = (Class<? extends WebPage>) Class.forName(webPageClassName);
			Constructor<? extends WebPage> webPageConst = webPageClass.getConstructor(HttpServletRequest.class);
			webPage = webPageConst.newInstance(request);
		} catch (Exception e) {
			webPage = new ApplicationHomePage(request);
		}
		response.getWriter().print(webPage);
	}
	
}
